﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using PetaPoco;
using HeimaSoft.Models;
using API.Controllers.AdminApi;

namespace HeimaSoft.Controllers.AdminApi
{
    [Route("AdminApi/{subFolder}/[action]")]
    [ApiController]
    public class {tblName}Controller  : BaseController
    {

		#region {tblName}模块

        public ActionResult<string> [tblName]list()
        {

            // fields: {strNames}
            Sql sql = new Sql("select * from [tblName]");
  [loop start]
			string [fldName] = GetString("[fldName]");
            if ([fldName] != "")
            {
                sql.Where("[fldName]=@0", [fldName]); //[fldTitle]
            }

  [loop end]^
            sql.OrderBy("id desc");

            //Success(sql);
            int pagesize = GetInt("pagesize");
            int pageindex = GetInt("pageindex");
            Page<{tblName}> list = db.Page<{tblName}>(pageindex, pagesize, sql);
            
            return Success(list);
        }

        public ActionResult<string> [tblName]view()
        {
            int id = GetInt("id");
            if (id <= 0)
            {
                var mod = new {tblName}(){ id = 0 };
                return Success(mod);
            }
            else
            {
                var mod = db.FirstOrDefault<{tblName}>("where id=@0", id);
                if (mod != null)
                {
                    return Success(mod);
                }
                else
                {
                    return Error("记录不存在");
                }
            }
        }
        public ActionResult<string> [tblName]edit()
        {
            int id = GetInt("id");
            var form = Request.Form;
            Dictionary<string, string> dic = new Dictionary<string, string>()
            {
  [loop start]
                { "[fldName]","[fldTitle]不能为空" },
  [loop end]^

            };
            if (id > 0)
            {
                #region 修改处理
                var errorMsg = ValidateCheck(dic);
                if (errorMsg != "")
                {
                    return ValidateError(errorMsg);
                }

                var mod = db.FirstOrDefault<{tblName}>("where id=@0", id);
                if (mod == null)
                {
                    return Error("要修改的记录不存在");
                }

                TryUpdateModelAsync<{tblName}>(mod);
				try
				{
					db.Update(mod);
				}
				catch (Exception ex)
				{
					return ValidateError(ex.Message);
				}
                return Success(mod);
                #endregion
            }
            else
            {
                #region 新增处理
                var errorMsg = ValidateCheck(dic);
                if (errorMsg != "")
                {
                    return ValidateError(errorMsg);
                }

                var mod = new {tblName}();
                TryUpdateModelAsync<{tblName}>(mod);
  [loop start]
                //mod.[fldName] = [fldDefaultValue];
  [loop end]^

				try
				{
					db.Insert(mod);
				}
				catch (Exception ex)
				{
					return ValidateError(ex.Message);
				}
                if (mod.id > 0)
                {
                    return Success(mod);
                }
                else
                {
                    return Error("数据插入失败");
                }
                #endregion
            }

        }


        public ActionResult<string> [tblName]delete()
        {
            int id = GetInt("id");
            if (id <= 0)
            {
                return Error("非法的id");
            }
            
			int rows = db.Delete<{tblName}>(id);
			if (rows > 0)
			{
                return Success("删除成功");
			}
			else
			{
                return Error("删除失败");
			}
           

        }
		#endregion


	}
}